
/* a lot diferent from prototype */
function $(element) { return document.getElementById(element); }

if (!Guia) 
{
	var Guia = new Object();
}

if (window.widget)
{
	widget.show = function() { }
	
	widget.hide = function() { }
	
	widget.onremove = function() { }
}

Guia.setupWidget = function() {
	Guia.doneButton = new AppleGlassButton($('doneButton'), 'Done', Guia.prefs.store);
	Guia.infoButton = new AppleInfoButton($('infoButton'), $('front'), 'white', 'white', Guia.prefs.set);
	Guia.refreshButton = new RefreshButton($("refreshButton"), $('front'), "Images/Refresh/", Guia.prefs.set);
	Guia.progress = new Guia.progressIndicator('progressGraphic');
	
	// Set up scrollbar
	Guia.scrollbar = new AppleVerticalScrollbar($('scrollbar'));
	Guia.scrollArea = new AppleScrollArea($('contents'));
	
	Guia.scrollArea.addScrollbar(Guia.scrollbar);
	
	// scrollBar settings
	Guia.scrollArea.focus();
	Guia.scrollsHorizontally = false;
	Guia.singlepressScrollPixels = 25;
	Guia.scrollbar.autohide = true;
	
	Guia.userInterface = new Guia.Control();
	
	//Guia.getDataForChannel("AXN",0);
}

Guia.Control = function() {
	this.currentDay = 0;
	this.currentChannel;
	
	this.data = new Guia.Calendar("weekDay", "date");
	
	this.left = new Guia.Arrow("left");
	this.right = new Guia.Arrow("right");
	
	this.left.hideFocus();
	this.data.print();
}

Guia.Control.prototype = {
	nextDay: function() {
		if ( this.currentDay == 6) {
			return 0;
		} 
		else if ( this.currentDay == 5 ) {
			this.right.hideFocus();
			this.currentDay = 6;
		}
		else this.currentDay++;
		
		this.data.nextDay();
		this.left.getFocus();
		
		/* pedir informacoes */
	},
	
	previousDay: function() { 
		if ( this.currentDay == 0) {
			return 0;
		} 
		else if ( this.currentDay == 1 ) {
			this.left.hideFocus();
			this.currentDay = 0;
		}
		else this.currentDay--;
		
		this.data.previousDay();
		this.right.getFocus();
	},
	
	refresh: function(channel) {
		
	}
}

Guia.getDataForChannel = function(channelId, weekDay) {
	// start feedback
	Guia.progress.start();

	var web = new URL();
	var parse = new ParseHtml();
	
	/* for some reason compression dont work */
	web.noCompression();
	
	web.location = "http://www.tvcabo.pt/TV/PrintProgTv.aspx?channelsigla=" + channelId + "&dia=" + weekDay + "&categoria=";
	web.fetchAsync(parseContents);
	
	function parseContents(web) {
		parse.contents =  web.result;
		
		parse.contents = parse.getElementById("_ctl0__ctl1_Datagrid1");
		
		information = parse.getElementsByTagName("td");
		
		listening = new String();
		
		var a = 0;
		for(var i=0; i < information.length; i+=2) {
			hour = Guia.utils.stripTags(information[i], true, "td", "font", "a","/a","/td","/font")
			program = Guia.utils.stripTags(information[i+1], true, "td", "font", "a","/a","/td","/font")
			
			listening += '<div class="' + (++a%2 ? 'even' : 'odd' )  + '">' + hour + ' ' + program + '</div>';
		}
		
		Guia.progress.stop();
		alert(listening)
		$("contents").innerHTML = listening;
		Guia.scrollbar.refresh();
		Guia.scrollArea.refresh();
	}
}

Guia.prefs = {
	set: function() {
		/* stuff here */
		Guia.show.back();
	},
	
	store: function() {
		/* stuff here */
		Guia.show.front();
	}
}

Guia.show = {
	front: function() {
		if (window.widget) widget.prepareForTransition("ToFront");

	    $('front').style.display = "block";
	    $('back').style.display = "none";
	
		if (window.widget) setTimeout ('widget.performTransition();', 0);
	},
	
	back: function() {
		if (window.widget) widget.prepareForTransition("ToBack");

	    $('front').style.display= "none";
	    $('back').style.display= "block";
	
		if (window.widget) setTimeout ('widget.performTransition();', 0);
	}
}

Guia.utils = {
	stripTags: function() {
		var string = arguments[ 0];
		var strip = arguments[ 1];
		for(var i = 2; i < arguments.length; i++)
		{
			var regex = new RegExp("<"+arguments[i]+"[^>]*>","img");
			string = string.replace(regex, strip ? "" : "<"+arguments[i]+">");
		}
		return string;
	}
	
}
Guia.Arrow = function(element) {
	this.element = $(element);
}

Guia.Arrow.prototype = {
	getFocus: function() {
		this.element.style.opacity = 1;
	},
	
	hideFocus: function() {
		this.element.style.opacity = 0.3;
	}
}

Guia.Calendar = function(weekTag,dateTag) {
	this.today = new Date();
	this.today = this.today.getDay();
	this.weekDay = 0;
	
	this.weekDayElement = $(weekTag);
	this.dateElement = $(dateTag);
	
	_self = this;
	
}

Guia.Calendar.prototype = {
	nextDay: function() {
		if ( this.weekDay == 6) {
			return 0;
		} 
		else if ( this.weekDay == 5 ) {
			this.weekDay = 6;
		} 
		else 
			this.weekDay++;

		this.print();
	},
	
	
	previousDay: function() {
		if ( this.weekDay == 0) {
			return 0;
		} 
		else if ( this.weekDay == 1 ) {
			this.weekDay = 0;
		} 
		else 
			this.weekDay--;

		this.print();
	},
	
	print: function() {
		//this.weekDayElement.innerHTML = this.printWeekDay();
		this.dateElement.innerHTML = this.printCalendarWithFormat();
	},
	
	printWeekDay: function() {
		
		
		if (this.weekDay == 0)
		{ 
			return "Hoje, ";
		} 
		return weekStrings[ (this.today+this.weekDay) % 7 ] + ", ";
	},
	
	printCalendarWithFormat: function() {
		var day = new Date();
		
		var months = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", 
			"Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];
			
		var weekStrings = ["Domingo","Segunda","Terça","Quarta", "Quinta","Sexta","Sábado"];
		
		day.setDate(day.getDate()+this.weekDay);
		
		var output;
		
		if (this.weekDay == 0)
		{ 
			output = "Hoje, ";
		} 
		else
		 	output = weekStrings[ (this.today+this.weekDay) % 7 ] + ", ";
		
		return output + day.getDate() + " de " + months[day.getMonth()];
	}
}

Guia.progressIndicator = function(element) {
	this.count = 0;
    this.timer = null;
    this.element = $(element);
    this.element.style.display = "none";
    this.imageBaseURL = "Images/Progress/prog";
}

Guia.progressIndicator.prototype = {
    start : function () {
        this.element.style.display = "block";
        if (this.timer) clearInterval(this.timer);
        this.tick();
        var localThis = this;
        this.timer = setInterval (function() { localThis.tick() }, 60);
    },

    stop : function () {
        clearInterval(this.timer);
        this.element.style.display = "none";
    },

    tick : function () {
        var imageURL = this.imageBaseURL + (this.count + 1) + ".png";
        this.element.src = imageURL;
        this.count = (this.count + 1) % 12;
    }
}

Guia.createKey = function(name) { return widget.identifier + '-' + name; }